package com.ebeitech.auction.push.huawei;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.ebeitech.auction.R;
import com.ebeitech.auction.constants.CommonConstants;
import com.ebeitech.auction.push.xiaomi.PushUtils;
import com.ebeitech.auction.view.homepage.HomepageActivity;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.Log;
import com.ebeitech.library.util.SPManager;
import com.huawei.android.pushagent.api.PushEventReceiver;
import com.huawei.android.pushagent.api.PushManager;

/*
 * 接收Push所有消息的广播接收器
 */
public class PushReceiverHuawei extends PushEventReceiver {
	private final static int SEND_MY_STATE = -1;

	@SuppressLint("NewApi")
	@Override
	public void onToken(Context context, String token, Bundle extras) {
		String belongId = extras.getString("belongId");
		String content = "获取token和belongId成功，token = " + token + ",belongId = " + belongId;

		PushManager.enableReceiveNotifyMsg(context, true);
		SPManager.getInstance().put(SPConstants.SHARED_PRE_CHANNEL_ID_KEY, token);
		SPManager.getInstance().put(SPConstants.SHARED_PRE_CHANNEL_STYLE, "huawei");
		Log.i("注册成功onToken" + content);
		boolean isLoginOffline = SPManager.getInstance().get(CommonConstants.IS_LOGIN_OFFLINE, true);
		if (!isLoginOffline) {
			PushUtils.sendMyStateUrl(context, SPConstants.LOGIN_STATE);
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
		try {
			String content = "收到一条Push消息： " + new String(msg, "UTF-8");
			Log.i(getClass(), "华为推送:收到一条推送 " + new String(msg, "UTF-8"));
//			
			// 如果处理前台，不处理第三方的推送
			boolean isAPPForeground = SPManager.getInstance().get(CommonConstants.IS_APP_FOREGROUND, true);
			if (isAPPForeground) {
				return false;
			}
        	
        	String[]contentMessages = content.split("@ebei@");
			//本地开启通知栏
			//消息通知栏
	        //定义NotificationManager
	        String ns = Context.NOTIFICATION_SERVICE;
	        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
	        //定义通知栏展现的内容信息
	        int icon = R.mipmap.ebei_ic_launcher;
	        CharSequence tickerText = contentMessages[0];
	        long when = System.currentTimeMillis();
	        Notification notification = new Notification(icon, tickerText, when);
	         
	        //定义下拉通知栏时要展现的内容信息
	        Context mcontext = context.getApplicationContext();
	        CharSequence contentTitle = contentMessages[0];
	        CharSequence contentText = contentMessages[1];
	        Intent notificationIntent = new Intent(context, HomepageActivity.class);
	        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);

			NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
			builder.setContentTitle(contentTitle)
					.setContentText(contentText)
					.setSmallIcon(R.mipmap.ebei_ic_launcher)
					.setTicker(tickerText)
					.setWhen(when)
					.setContentIntent(contentIntent);

	        //用mNotificationManager的notify方法通知用户生成标题栏消息通知
	        mNotificationManager.notify((int)(Math.random()*100), notification);
	        
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public void onEvent(Context context, Event event, Bundle extras) {
		if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
			int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
			if (0 != notifyId) {
				NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
				manager.cancel(notifyId);
			}
			String content = "收到通知附加消息： " + extras.getString(BOUND_KEY.pushMsgKey);
			Log.i("onEvent" + content);
		} else if (Event.PLUGINRSP.equals(event)) {
			final int TYPE_LBS = 1;
			final int TYPE_TAG = 2;
			int reportType = extras.getInt(BOUND_KEY.PLUGINREPORTTYPE, -1);
			boolean isSuccess = extras.getBoolean(BOUND_KEY.PLUGINREPORTRESULT,false);
			String message = "";
			if (TYPE_LBS == reportType) {
				message = "LBS report result :";
			} else if (TYPE_TAG == reportType) {
				message = "TAG report result :";
			}
			Log.i("onEvent" + message + isSuccess);
		}
		super.onEvent(context, event, extras);
	}

}
